package com.wenlin.myproduction.util;

import android.content.Context;
import android.os.Build;

import com.wenlin.myproduction.MyApplication;
import java.io.File;

/**
 * 文件路径的获取和拼接
 */
public class FilePathUtil {
    public static final String AUDIO_DIR_PATH = "Pictures/wenlin/audioRecorderEx";
    public static final String DEVICE_PATH = "Pictures/wenlin/device";
    public static final String IM_PATH = "Pictures/wenlin/im";
    public static final String ALPHA_VIDEO_PATH = "alpha_video";
    public static final String COMPRESS_PATH = "Pictures/wenlin/compress";//视频压缩缓存地址，发完就可以删除的
    public static final String VISITOR_SUFFIX = ".jpg";//保存游客token的后缀

    public static String getAlphaVideoPreloadPath() {
        File file = getCacheFolderFile(MyApplication.getMyApplicationContext(), ALPHA_VIDEO_PATH, false);
        StringBuilder absoluteFolderPath = new StringBuilder(file.getAbsolutePath());
        if (!absoluteFolderPath.toString().endsWith("/")) {
            absoluteFolderPath.append("/");
        }
        return absoluteFolderPath.toString();
    }

    public static String getImPath() {
        File file = getCacheFolderFile(MyApplication.getMyApplicationContext(), IM_PATH, false);
        StringBuilder absoluteFolderPath = new StringBuilder(file.getAbsolutePath());
        if (!absoluteFolderPath.toString().endsWith("/")) {
            absoluteFolderPath.append("/");
        }
        return absoluteFolderPath.toString();
    }

    /**
     * 获取压缩视频的完整路径，带文件名字
     *
     * @param oriPath 原视频的完整路径
     * @return 所求
     */
    public static String getCompressFileAbsolutePath(String oriPath) {
        String fileName = Checker.hasWord(oriPath) ? oriPath : "temp";
        if (Checker.hasWord(oriPath) && oriPath.contains(File.separator)) {
            String[] arr = oriPath.split(File.separator);
            fileName = arr[arr.length - 1];
        }
        return getCompressPath() + fileName;
    }

    /**
     * 获取压缩视频缓存的路径
     *
     * @return 所求
     */
    public static String getCompressPath() {
        File file = getCacheFolderFile(MyApplication.getMyApplicationContext(), COMPRESS_PATH, false);
        StringBuilder absoluteFolderPath = new StringBuilder(file.getAbsolutePath());
        if (!absoluteFolderPath.toString().endsWith("/")) {
            absoluteFolderPath.append("/");
        }
        return absoluteFolderPath.toString();
    }

    /**
     * 生成存储文件的路径，如果有sd卡则获取sd卡路径，否则获取应用缓存区路径。
     *
     * @param context    应用Context
     * @param folderPath 文件夹路径
     * @param fileName   文件名
     * @param isToSDCard 是否必须需要写入到SD卡,保存游客id时填true，其他情况为false
     * @return 生成的文件路径
     */
    public static String makeFilePath(Context context, String folderPath, String fileName, boolean isToSDCard) {
        File file = getCacheFolderFile(context, folderPath, isToSDCard);
        StringBuilder absoluteFolderPath = new StringBuilder(file.getAbsolutePath());
        if (!absoluteFolderPath.toString().endsWith("/")) {
            absoluteFolderPath.append("/");
        }
        if (fileName != null) {
            absoluteFolderPath.append(fileName);
        }
        return absoluteFolderPath.toString();
    }

    /***
     * 获取缓存文件夹以File形式返回
     * @param folderPath 文件夹路径
     * @param isToSDCard true 一定写入pictures，false 写入缓存中
     * */
    private static File getCacheFolderFile(Context context, String folderPath, boolean isToSDCard) {
        File file = null;
        if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
            if (!isToSDCard && Build.VERSION.SDK_INT > 29) {
                file = new File(context.getExternalFilesDir(null), folderPath);
            } else {
                file = new File(android.os.Environment.getExternalStorageDirectory(), folderPath);
            }
        } else {
            file = context.getApplicationContext().getCacheDir();
        }
        if (!file.exists() || !file.isDirectory()) {
            file.mkdirs();
        }
        return file;
    }

    /**
     * 清空某一路径下的文件
     *
     * @param context
     * @param filePath
     */
    public static void clearFilePath(Context context, File filePath) {
        if (!filePath.exists()) {
            return;
        }
        if (filePath.isFile()) {
            filePath.delete();
            return;
        }
        if (filePath.isDirectory()) {
            File[] folders = filePath.listFiles();
            for (int i = 0; i < folders.length; i++) {
                clearFilePath(context, folders[i]);
            }
        }
    }

    //生成文件
    public static File makeFilePath(String filePath) {
        File file = null;
        try {
            file = new File(filePath);
            if (!file.exists()) {
                file.createNewFile();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return file;
    }

    /***写入游客登录id*/
    public static void setTxtDeviceLogin(Context context, String str) {
        if (!str.equals(getTxtDeviceLogin(context))) {//不一样的,生成新的
            try {
                String filePath = makeFilePath(context, DEVICE_PATH, RandomUtil.get3Random() + str + VISITOR_SUFFIX, true);
                makeFilePath(filePath);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /***得到游客登录id*/
    public static String getTxtDeviceLogin(Context context) {
        String content = "";
        File f = getCacheFolderFile(context.getApplicationContext(), DEVICE_PATH, true);
        if (f.isDirectory()) {
            File[] folders = f.listFiles();
            if (folders != null && folders.length > 0) {
                long time = 0L;//得到文件的创建时间
                for (int i = folders.length - 1; i > -1; i--) {
                    File file = folders[i];
                    if (file.lastModified() > time) {
                        time = file.lastModified();
                        content = file.getName();//因为安卓13,不能读取txt文件和里面的内容,所有直接取他的名字
                        if (content.contains(VISITOR_SUFFIX) && folders[i].length() == 0) {
                            content = content.substring(0, content.length() - 4);
                        }
                    }
                }
            }
        }
        if (Checker.hasWord(content) && content.length() > 2) {
            content = content.substring(3);
        }
        return content;
    }
}
